﻿(function($) {
    $.extend({ openModal: function(idControl, attributes) {
        $modal = $('#modal_' + idControl);

        if ($modal.length == 0) {
            $baseControl = $('#' + idControl).css('display', '');
            $containerPop = $.create('div', { id: 'content_' + idControl }).addClass(attributes.contentCss).css('width', $baseControl.width());
            var pg = $.getPageSize();
            var $cmdBar = $.create('div', { id: 'cmdTool_' + idControl }).css('text-align', 'right');

            $modal = $.create('div', { id: 'modal_' + idControl }).addClass(attributes.modalCss).css('height', pg.Height).css('width', pg.Width).css('display', 'none');

            if (attributes.minimizedButton) {
                if (attributes.minimizedButton.visible) {
                    var $min = $.createCommandButton('javascript:void($("#' + $containerPop.attr('id') + '").minimized("' + attributes.title + '", "' + attributes.IconClass + '"))', 'Riduci a icona', attributes.minimizedButton.normal, attributes.minimizedButton.hover, '_');
                    $cmdBar.append($min);
                }
            }

            if (attributes.maximizedButton) {
                if (attributes.maximizedButton.visible) {
                    var $restore = $.createCommandButton('#', 'Ripristino Dimensioni', attributes.maximizedButton.restored.normal, attributes.maximizedButton.restored.hover, '*').css('display', 'none');
                    var $max = $.createCommandButton('#', 'Ingrandisci', attributes.maximizedButton.normal, attributes.maximizedButton.hover, '[]').click(function() {
                        $('#' + $containerPop.attr('id')).maximized();
                        $(this).css('display', 'none');
                        $restore.css('display', '');
                    });
                    $restore.click(function() {
                        $('#' + $containerPop.attr('id')).normalized();
                        $(this).css('display', 'none');
                        $max.css('display', '');
                    });
                    $cmdBar.append($restore);
                    $cmdBar.append($max);
                }
            }

            if (attributes.closeButton) {
                if (attributes.closeButton.visible) {
                    var $close = $.createCommandButton('javascript:void($.closeModal("' + idControl + '"))', 'Chiudi', attributes.closeButton.normal, attributes.closeButton.hover, 'X');
                    $cmdBar.append($close);
                }
            }

            if ($cmdBar.children().length != 0) {
                $containerPop.append($cmdBar);
            }

            $containerPop.setAbsolutePosition($baseControl.calcCenterPosition());
            $modal.setAbsolutePosition({ Top: 0, Left: 0 });
            $containerPop.append($baseControl.clone());
            $baseControl.remove();
            $modal.append($containerPop);

            $('body').append($modal);
        }

        $modal.fadeIn('slow');

        return $modal;
    }
    });
})(jQuery);

(function($) {
    $.extend({ closeModal: function(idControl) {
        var $modal = $('#modal_' + idControl);

        if ($modal.lenght != 0) {
            $modal.fadeOut('fast');
        }
    }
    });
})(jQuery);

(function($) {
    $.extend({ createCommandButton: function(href, title, normal, hover, alternateText) {

        var $a = $.create('a', { href: href, title: title });

        if (normal != undefined && hover != undefined) {
            $a.append($.create('img', { src: normal, border: 0 }).hover(
                                function(e) {
                                    $(this).attr('src', hover);
                                },
                                function(e) {
                                    $(this).attr('src', normal);
                                }));
        } else {
            $a.html(alternateText);
        }

        return $a;
    }
    });
})(jQuery)